package com.tiancheng.trade.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.payment.enums.CouponSettleBusinessTypeEnum;
import com.tiancheng.trade.commom.core.enums.SettleStatusEnum;
import com.tiancheng.trade.payment.mapper.CouponSettleFileMapper;
import com.tiancheng.trade.payment.model.CouponSettleDetail;
import com.tiancheng.trade.payment.model.CouponSettleFile;
import com.tiancheng.trade.payment.service.CouponSettleDetailService;
import com.tiancheng.trade.payment.service.CouponSettleFileService;
import com.tiancheng.trade.payment.task.ums.bo.CouponSettleFileBo;
import com.tiancheng.trade.payment.vo.admin.CouponSettleFileQueryVO;
import com.tiancheng.trade.payment.vo.admin.res.CouponSettleFileResVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Author: likailun
 * @Description: 优惠券清分汇总service
 * @Date: create in 2021/12/17 3:18 下午
 */
@Service
public class CouponSettleFileServiceImpl implements CouponSettleFileService {

    @Resource
    private CouponSettleFileMapper couponSettleFileMapper;
    @Resource
    private CouponSettleDetailService couponSettleDetailService;

    @Override
    public void save(CouponSettleFile couponSettleFile) {
        couponSettleFileMapper.insert(couponSettleFile);
    }

    @Override
    public List<CouponSettleFile> queryByFileNames(Set<String> fileNames) {
        return couponSettleFileMapper.selectList(new LambdaQueryWrapper<CouponSettleFile>().in(CouponSettleFile::getFileName, fileNames));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveCouponSettleResult(List<CouponSettleFileBo> couponSettleFileBos) {
        // 1、更新汇总表信息
        if (CollectionUtils.isNotEmpty(couponSettleFileBos)) {
            for (CouponSettleFileBo couponSettleFileBo : couponSettleFileBos) {
                CouponSettleFile updateEntity = new CouponSettleFile();
                updateEntity.setId(couponSettleFileBo.getId());
                updateEntity.setUpdateDate(new Date());
                updateEntity.setTransferOutScNum(0);
                updateEntity.setTransferOutScAmount(0);
                updateEntity.setTransferOutErNum(0);
                updateEntity.setTransferOutErAmount(0);

                updateEntity.setTransferInScNum(0);
                updateEntity.setTransferInScAmount(0);
                updateEntity.setTransferInErNum(0);
                updateEntity.setTransferInErAmount(0);
                updateEntity.setTransferOutErAmount(0);
                if (!couponSettleFileBo.isCheckSuccess()) {
                    // 银联返回清分文件校验失败
                    couponSettleDetailService.settleError(couponSettleFileBo.getFileName(), "银联校验清分文件失败");
                    // 更新明细项、支付优惠、退款优惠清分失败
                    continue;
                }
                if (CollectionUtils.isNotEmpty(couponSettleFileBo.getSettleDetails())) {
                    couponSettleDetailService.updateSettleStatus(couponSettleFileBo.getSettleDetails());
                }
                List<CouponSettleDetail> couponSettleDetails = couponSettleDetailService.queryByFileName(couponSettleFileBo.getFileName());
                if (CollectionUtils.isNotEmpty(couponSettleDetails)) {
                    // 转出
                    couponSettleDetails.stream().filter(c -> c.getBusinessType().equals(CouponSettleBusinessTypeEnum.TRANSFER_OUT.getCode())).forEach(c -> {
                        if (c.getSettleStatus().equals(SettleStatusEnum.SUCCESS.getCode())) {
                            //转出成功
                            updateEntity.setTransferOutScNum(updateEntity.getTransferOutScNum() + 1);
                            updateEntity.setTransferOutScAmount(updateEntity.getTransferOutScAmount() + c.getSettleCouponAmount());
                        } else if (c.getSettleStatus().equals(SettleStatusEnum.FAIL.getCode())) {
                            //转出成功
                            updateEntity.setTransferOutErNum(updateEntity.getTransferOutErNum() + 1);
                            updateEntity.setTransferOutErAmount(updateEntity.getTransferOutErAmount() + c.getSettleCouponAmount());
                        }
                    });

                    couponSettleDetails.stream().filter(c -> c.getBusinessType().equals(CouponSettleBusinessTypeEnum.TRANSFER_IN.getCode())).forEach(c -> {
                        if (c.getSettleStatus().equals(SettleStatusEnum.SUCCESS.getCode())) {
                            //转入成功
                            updateEntity.setTransferInScNum(updateEntity.getTransferInScNum() + 1);
                            updateEntity.setTransferInScAmount(updateEntity.getTransferInScAmount() + c.getSettleCouponAmount());
                        } else if (c.getSettleStatus().equals(SettleStatusEnum.FAIL.getCode())) {
                            //转入成功
                            updateEntity.setTransferInErNum(updateEntity.getTransferInErNum() + 1);
                            updateEntity.setTransferInErAmount(updateEntity.getTransferInErAmount() + c.getSettleCouponAmount());
                        }
                    });
                    couponSettleFileMapper.updateById(updateEntity);
                }
            }
        }
    }

    @Override
    public PageInfo<CouponSettleFileResVo> findList(CouponSettleFileQueryVO couponSettleFileQueryVO) {
        return null;
    }

    @Override
    public void exportList(CouponSettleFileQueryVO couponSettleFileQueryVO, HttpServletResponse response, HttpServletRequest request) {

    }
}
